package com.example.transformpage;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.baselibrary.libs.Utils.log.LogUtils;
import com.example.transformpage.NoHttp.controlQueue.ControlQueue;
import com.example.transformpage.NoHttp.controlQueue.DownTimer;
import com.example.transformpage.NoHttp.queue.Request;
import com.example.transformpage.NoHttp.task.NetworkInterface_plan;
import com.example.transformpage.View.GalleryView;
import com.example.transformpage.View.PlanButton1_new;

import java.util.ArrayList;

public class MainActivity extends Activity implements View.OnClickListener {
    private ViewPager viewPager;
    private ViewPaperAdapter vpAdapter;
    private GalleryView mGalleryView;
    private ControlQueue mMControlQueue;
    private ArrayList<RelativeLayout> mViews;

    @Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.main);
        mMControlQueue = ControlQueue.getControlQueue(100);
        mMControlQueue.start();
        initView();

	}

    private void initView() {
        mGalleryView = (GalleryView)findViewById(R.id.galleryView);
        initData();
    }

    private void initData() {
        mViews = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            RelativeLayout view1 = (RelativeLayout) View.inflate(this, R.layout.page, null);
            PlanButton1_new pb = (PlanButton1_new) view1.findViewById(R.id.pb);
            //点击的时候好找到
            view1.setTag(i);
            view1.setOnClickListener(this);
            pb
                    .setData_id(i + "")
                    .setItemId(i)
                    .setData_tv4(1000 * 1000)

            ;
            mViews.add(view1);
            Request request = new Request();
            request.setId(i);
            request.setTimer(1000 * 1000);
            //测试
            request.setTimer(100000);
            mMControlQueue.setDump(false);
            mMControlQueue.add(request);

        }

//        //实例化各个界面的布局对象
//        View view1 = View.inflate(this, R.layout.plan_button1, null);
//        View view2 = View.inflate(this, R.layout.plan_button1, null);
//        View view3 = View.inflate(this, R.layout.plan_button1, null);
//        View view4 = View.inflate(this, R.layout.plan_button1, null);
//
//        ((ImageView)view1.findViewById(R.id.image)).setImageResource(R.drawable.image01);
//        ((ImageView)view2.findViewById(R.id.image)).setImageResource(R.drawable.image02);
//        ((ImageView)view3.findViewById(R.id.image)).setImageResource(R.drawable.image03);
//        ((ImageView)view4.findViewById(R.id.image)).setImageResource(R.drawable.image02);


//        views.add(view1);
//        views.add(view2);
//        views.add(view3);
//        views.add(view4);


        mGalleryView
                .setScale(85)
                .setSize(this, 90)
                .setData(this, mViews);


        mMControlQueue.setCallBack(new NetworkInterface_plan<DownTimer, Integer>() {
            @Override
            public void onResult(DownTimer downTimer) {
                LogUtils.i("正常每次回来的东西"+"\tdownTimer.getId()\t"+downTimer.getId()+"\tdownTimer.getTime()\t"+downTimer.getTime());
                for (int i = 0; i < mViews.size(); i++) {
                    LogUtils.i("倒计时回来什么"+"\tdownTimer.getId()\t"+downTimer.getId()+"\tdownTimer.getTime()\t"+downTimer.getTime());
                    RelativeLayout view = (RelativeLayout)mViews.get(i);
                    if (view.getChildAt(0) instanceof PlanButton1_new){
                        PlanButton1_new button1 = (PlanButton1_new)view.getChildAt(0);
                        LogUtils.i("回来的数据是什么"+"\tdownTimer.getId()\t"+downTimer.getId()+"\tbutton1.getData_id()\t"+button1.getData_id());
                        if (downTimer.getId()==button1.getItemId()){
                            button1.setData_tv4(downTimer.getTime());

//                        Timers timers = timerses.get(downTimer.getId());
//                        timers.setTime(downTimer.getTime());


                            if (downTimer.getTime()>0){
                                Request request = new Request();
                                request.setId(downTimer.getId());
                                request.setTimer(downTimer.getTime());
                                //倒计时_测试
                                //AppContext.setTimeDaojishi((int) downTimer.getTime());

                                mMControlQueue.add(request);
                            }
                        }
                    }

                }
            }

            @Override
            public void onUpData(Integer integer) {

            }
        });
    }

    //双击退出程序
    private long firstPressTime = 0;

    @Override
    public void onBackPressed() {
        long now = System.currentTimeMillis();
        if ((now - firstPressTime) > 2000) {
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            firstPressTime = now;
        } else {
            finish();
            System.exit(0);
        }
    }

    @Override
    public void onClick(View v) {
        for (int i = 0; i < mViews.size(); i++) {
            if ((int)v.getTag() == i){
                LogUtils.i("我点了"+i);

                return;
            }
        }
    }
}
